package com.zdsoft.finance.product.repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.zdsoft.finance.product.entity.ProductRiskRule;
import com.zdsoft.framework.core.common.domain.BaseEntity;
import com.zdsoft.framework.core.common.util.ObjectHelper;

/**
 * 风险规则自定义操作仓库实现
 * @author longwei
 * @date 2016/11/22
 * @version 1.0
 */
public class ProductRiskRuleRepositoryImpl {
	//查询头部
	private StringBuffer hqlBuffer = new StringBuffer("from ProductRiskRule where 1=1 ");
	
	@PersistenceContext
	private EntityManager em;
	
	/**
	 * 通过对象查询并分页
	 * @param productRiskRule 查询风险规则对象
	 * @param pageable 分页信息
	 * @return 分页结果
	 */
	@SuppressWarnings("unchecked")
	public Page<ProductRiskRule> queryByCons(ProductRiskRule productRiskRule, Pageable pageable){
		//条件组装
		StringBuffer whereHqlBuffer = new StringBuffer(" and isDeleted=:isDeleted");
		if(ObjectHelper.isNotEmpty(productRiskRule.getRule())){
			whereHqlBuffer.append(" and rule like :rule ");
		}
		if(ObjectHelper.isNotEmpty(productRiskRule.getParam())){
			whereHqlBuffer.append(" and param like :param");
		}
		if(ObjectHelper.isNotEmpty(productRiskRule.getApplyingSceneCd())){
			whereHqlBuffer.append(" and applyingSceneCd like :applyingSceneCd");
		}
		if(ObjectHelper.isNotEmpty(productRiskRule.getProduct().getId())){
			whereHqlBuffer.append(" and product.id = :productId");
		}
		whereHqlBuffer.append(" order by createTime desc");
		
		//参数组装
		Query query = em.createQuery(this.hqlBuffer.toString() + whereHqlBuffer.toString());
		Query countQ = em.createQuery("select count(*) " + this.hqlBuffer.toString() + whereHqlBuffer.toString());
		query.setParameter("isDeleted",!BaseEntity.DELETED);
		countQ.setParameter("isDeleted", !BaseEntity.DELETED);
		
		if(ObjectHelper.isNotEmpty(productRiskRule.getRule())){
			query.setParameter("rule","%"+productRiskRule.getRule()+"%");
			countQ.setParameter("rule", "%"+productRiskRule.getRule()+"%");
		}
		if(ObjectHelper.isNotEmpty(productRiskRule.getParam())){
			query.setParameter("param","%"+productRiskRule.getParam()+"%");
			countQ.setParameter("param", "%"+productRiskRule.getParam()+"%");
		}
		if(ObjectHelper.isNotEmpty(productRiskRule.getApplyingSceneCd())){
			query.setParameter("applyingSceneCd","%"+productRiskRule.getApplyingSceneCd()+"%");
			countQ.setParameter("applyingSceneCd","%"+ productRiskRule.getApplyingSceneCd()+"%");
		}
		if(ObjectHelper.isNotEmpty(productRiskRule.getProduct()) && ObjectHelper.isNotEmpty(productRiskRule.getProduct().getId())){
			query.setParameter("productId",productRiskRule.getProduct().getId());
			countQ.setParameter("productId",productRiskRule.getProduct().getId());
		}
		
		//查询
		query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
		query.setMaxResults(pageable.getPageSize());
		Page<ProductRiskRule> pager = new PageImpl<>(query.getResultList(),pageable,(Long)countQ.getSingleResult());
		return pager;
	}
}
